package com.longtop.framework.sys.flow.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.Constant;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.flow.model.TSysFlowNode;
import com.longtop.framework.sys.flow.model.TSysFlowParam;
import com.longtop.framework.sys.flow.model.TSysFlowRoute;

/**
 * 
 * @author 杨向阳
 * 
 */

public class FlowNodeService extends BaseService {
	public static final String BEAN_ID = "flowNodeService";

	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param changeMap
	 *            对象列表，有三个值，删除主键列表、更新bean的列表、新增bean的列表
	 * @param sessionObject
	 * @return 操作成功受影响的记录数，有三个值，插入成功的记录数、更新成功的记录数、删除成功的记录数
	 */
	public Integer[] saveFlowNode(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysFlowNode tSysFlowNode;
		String templateId = "";		//模板id
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				tSysFlowNode = (TSysFlowNode) iterator.next();
				
				templateId = tSysFlowNode.getTemplateId();	//模板id赋值,后面校验要用
				
				super.save(tSysFlowNode);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				tSysFlowNode = (TSysFlowNode) iterator.next();
				
				templateId = tSysFlowNode.getTemplateId();	//模板id赋值,后面校验要用
				
				super.update(tSysFlowNode);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysFlowNode where nodeId in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}
		
		//校验只能有一个 启动节点。
		if(insertCount > 0 || updateCount> 0) {
			
			List list = this.queryAll("from TSysFlowNode where templateId='" + templateId + "' and nodeType='1'");
	        if(list.size() > 1)
	        	throw new BaseCheckedException("", "保存节点失败,每个模板只能有一个启动节点!");
	       
	        list = this.queryAll("from TSysFlowNode where templateId='" + templateId + "' and nodeType='9'");
	        if(list.size() > 1)
	        	throw new BaseCheckedException("", "保存节点失败,每个模板只能有一个结束节点!");
	        
	        list = this.queryAll("from TSysFlowNode where templateId='" + templateId + "' and nodeType='8'");
	        if(list.size() > 1)
	        	throw new BaseCheckedException("", "保存节点失败,每个模板只能有一个被驳回到启动节点!");
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}
	
	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param changeMap
	 *            对象列表，有三个值，删除主键列表、更新bean的列表、新增bean的列表
	 * @param sessionObject
	 * @return 操作成功受影响的记录数，有三个值，插入成功的记录数、更新成功的记录数、删除成功的记录数
	 */
	public Integer[] saveFlowRoute(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysFlowRoute templateRoute;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				templateRoute = (TSysFlowRoute) iterator.next();
				super.save(templateRoute);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				templateRoute = (TSysFlowRoute) iterator.next();
				super.update(templateRoute);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysFlowRoute where routeId in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}
	
	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param changeMap
	 *            对象列表，有三个值，删除主键列表、更新bean的列表、新增bean的列表
	 * @param sessionObject
	 * @return 操作成功受影响的记录数，有三个值，插入成功的记录数、更新成功的记录数、删除成功的记录数
	 */
	public Integer[] saveFlowParam(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysFlowParam templateRouteParam;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				templateRouteParam = (TSysFlowParam) iterator.next();
				super.save(templateRouteParam);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				templateRouteParam = (TSysFlowParam) iterator.next();
				super.update(templateRouteParam);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysFlowParam where paramId in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}
}